package com.youxuan.ssyx;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

// 任务组合
public class CompletableFutureDemo5 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        // 任务 1 返回结果 1024
        CompletableFuture<Integer> futureA = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"----当前线程开始");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            int value = 1024;
            System.out.println("任务 1:"+value);
            System.out.println(Thread.currentThread().getName()+"----当前线程结束");
            return value;
        }, executorService);

        // 任务 2 返回结果 200
        CompletableFuture<Integer> futureB = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"----当前线程开始");
            int value = 200;
            System.out.println("任务 1:"+value);
            System.out.println(Thread.currentThread().getName()+"----当前线程结束");
            return value;
        }, executorService);

        CompletableFuture<Void> allFuture = CompletableFuture.allOf(futureA, futureB);
        allFuture.get();
        System.out.println(" 线程结束");
    }
}
